package com.heima.wemedia.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmUserThreadUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 自媒体照片素材上传
     *
     * @param multipartFile
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //参数校验
        if(multipartFile == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //登录校验
        Integer userId = WmUserThreadUtil.get();
        if(userId == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        //上传到minIO并返回url
        String originalFilename = multipartFile.getOriginalFilename();
        String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = null;
        try {
            url = fileStorageService.uploadImgFile("", UUID.randomUUID() + fileName, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //封装数据返回
        WmMaterial material = new WmMaterial();
        material.setUserId(userId);
        material.setUrl(url);
        material.setType((short) 0);
        material.setIsCollection((short) 0);
        material.setCreatedTime(new Date());
        save(material);

        return ResponseResult.okResult(material);
    }

    /**
     * 照片素材列表查询
     *
     * @param dto
     */
    @Override
    public ResponseResult lists(WmMaterialDto dto) {
        //参数校验
        dto.checkParam();
        if(dto == null || (dto.getIsCollection() != 0 && dto.getIsCollection() != 1))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //登录校验
        Integer userId = WmUserThreadUtil.get();
        if(userId == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        //分页查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> eq = Wrappers.<WmMaterial>lambdaQuery()
                .eq(WmMaterial::getIsCollection, dto.getIsCollection())
                .eq(WmMaterial::getUserId,userId);
        page(page,eq);

        //封装数据返回
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }


}
